<template>
  <div class="app-container">
    <el-form :model="pd.queryParams" ref="queryRef" :inline="true" v-show="pd.showSearch" label-width="68px">
      <el-form-item label="物品名称" prop="bomName">
        <el-input
          v-model="pd.queryParams.bomName"
          placeholder="请输入物品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="enableFlag">
          <el-switch v-model="pd.queryParams.enableFlag" active-text="启用" inactive-text="停用"
                     active-value="0" inactive-value="1"></el-switch>
        </el-form-item>
      <el-form-item>
      <el-form-item label="更新时间" style="width: 308px">
        <el-date-picker v-model="pd.daterangeUpdate_time" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['wms:bom:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="pd.single"
          @click="handleUpdate"
          v-hasPermi="['wms:bom:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="pd.multiple"
          @click="handleDelete"
          v-hasPermi="['wms:bom:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Upload"
          @click="handleUpload"
          v-hasPermi="['wms:bom:add']"
        >导入</el-button>
      </el-col>     
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['wms:bom:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="pd.showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table 
     v-loading="pd.loading" 
     :data="pd.bomList" 
     @selection-change="handleSelectionChange"
     >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物品名称" align="center" prop="name" />
      <el-table-column label="是否启用" align="center" prop="enableFlag">
        <template #default="scope">
          <dict-tag :options="sys_is" :value="scope.row.enableFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="180" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wms:bom:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['wms:bom:remove']">删除</el-button>
                    <el-button link type="primary" icon="Plus" @click="handleBP(scope.row)" >物料工序</el-button>
        </template>
      </el-table-column>
    </el-table>

    <BomCurd ref ="bomCurdRef" @ok="bomCurdOk"></BomCurd>
    
    <pagination
      v-show="pd.total>0"
      :total="pd.total"
      v-model:page="pd.queryParams.pageNum"
      v-model:limit="pd.queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Bom">
import BomCurd from "@/views/wms/bom/curd"
import { bomGetPage, getBom } from "@/api/wms/bom";
import { getCurrentInstance, reactive } from "vue";
import { useRoute } from "vue-router";

    const { proxy } = getCurrentInstance();
const { sys_is} = proxy.useDict('sys_is');
const route = useRoute();

onMounted(() => initPage());

const pd = reactive({
  bomList: [],
  loading: true,
  total: 0,
  daterangeUpdateTime: [],
  showSearch: true,
  ids: [],
  form: {},
  single: true,
  multiple: true,
  operation: 0,
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    productCode: null,
    bomName: null,
    enableFlag: "0",
  }
});

/** 查询生产物品列表 */
function getList() {
  pd.loading = true;
  if (null != pd.daterangeUpdateTime && '' != pd.daterangeUpdateTime) {
    pd.queryParams.beginTime = pd.daterangeUpdateTime[0];
    pd.queryParams.endTime = pd.daterangeUpdateTime[1];
  }
  debugger
  bomGetPage(pd.queryParams).then(response => {
    pd.bomList = response.rows;
    pd.total = response.total;
    pd.loading = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  pd.queryParams.bomName =  null;
  pd.daterangeUpdateTime = [];
  pd.queryParams.enableFlag = "0"
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  pd.ids = selection.map(item => item.productId);
  pd.single = selection.length != 1;
  pd.multiple = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.$refs["bomCurdRef"].add();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.productId || pd.ids[0]
  getBom(_id).then(response => {
    proxy.$refs["bomCurdRef"].update(response.data);
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  if (row!=null&&row.productId) {
    const _ids = []
    _ids[0] = row.productId
    proxy.$refs["bomCurdRef"].del(_ids)
  }else{
    proxy.$refs["bomCurdRef"].del(pd.ids)
  }
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('mes/bom/export', {
    ...pd.queryParams
  }, `bom_${new Date().getTime()}.xlsx`)
}

//刷新页面
function bomCurdOk() {
  getList();
}

function initPage() {
  pd.queryParams = { ...pd.queryParams, ...route.query };
  getList();
}

//添加物料工序
function handleBP(row) {
  proxy.$refs["bomCurdRef"].addBP(row);
}

getList();
</script>
